#include "../inc/Chess/rook.h"
#include <iostream>
using namespace std;

Rook::Rook(PieceType type, int row, int col, bool isRed)
    : Piece(type, row, col, isRed)
{
}

bool Rook::is_valid_move(int to_row, int to_col, PieceType board[9][10])
{
    int row = position.first;
    int col = position.second;

    // 计算移动距离
    int row_diff = to_row - row;
    int col_diff = to_col - col;

    // 车是直线移动
    if (row_diff != 0 && col_diff != 0)
    {
        return false;
    }

    // 计算路径上的棋子数量
    int piece_count = 0;

    // 横向移动
    if (row_diff == 0)
    {
        int start_col = min(col, to_col);
        int end_col = max(col, to_col);

        // 计算路径上的棋子数量（不包括起点和终点）
        for (int c = start_col + 1; c < end_col; c++)
        {
            if (board[row][c] != PieceType::kong)
            {
                piece_count++;
            }
        }
    }
    // 纵向移动
    else if (col_diff == 0)
    {
        int start_row = min(row, to_row);
        int end_row = max(row, to_row);

        // 计算路径上的棋子数量（不包括起点和终点）
        for (int r = start_row + 1; r < end_row; r++)
        {
            if (board[r][col] != PieceType::kong)
            {
                piece_count++;
            }
        }
    }

    return  piece_count == 0;
}